
#include "snake.h"
#include "ui_snake.h"
#include<QPainter>
#include<QTimer>
#include<QKeyEvent>



Snake::Snake(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::Snake),blsRun(false),Speed(500)
{
    ui->setupUi(this);//1
    this->setGeometry(QRect(600,300,290,310));
}

Snake::~Snake()
{
    delete ui;
}
void Snake::paintEvent(QPaintEvent*event){
    QPainter painter(this);
    if(!blsRun)
    {
        InitSnake();
    }
    //画游戏背景
    //外墙
    painter.setPen(Qt::black);//设置画笔颜色
    painter.setBrush(Qt::gray);//设置填充颜色
    painter.drawRect(15,15,260,260);

    //内墙
    painter.setPen(Qt::black);//设置画笔颜色
    painter.setBrush(Qt::black);//设置填充颜色
    painter.drawRect(20,20,250,250);
    painter.drawPixmap(20,20,250,250,QPixmap(":/"));

    //画格子
    painter.setPen(Qt::gray);
    for(int i=2;i<=27;i++){
        painter.drawLine(20,i*10,270,i*10);//画横线
        painter.drawLine(i*10,20,i*10,270);//画竖线
    }
    //显示游戏开始和结束
    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(40,150,Display);
    //显示分数
    QFont font2("Courier",15);
    painter.setFont(font1);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(140,300,"得分：");
    painter.drawText(230,300,QString::number(Score));

    //画蛇
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    //painter.drawRect(vSnakeRect);
    painter.drawRect(Food);
    painter.drawRects(&vSnakeRect[0],vSnakeRect.size());
    if(blsOver){
        timer->stop();
    }

}
void Snake::InitSnake(){
    blsRun=true;
    blsOver=false;
    nDirection=2;
    Display="游戏开始！";//刷新频率太快，显示不出来
    Score=0;
    Food=CreateFood();
    //QRect rect(100,70,10,10);
    //vSnakeRect=rect;
    vSnakeRect.resize(5);//蛇的长度
    for(int i=0;i<vSnakeRect.size();i++){
        QRect rect(100,70+10*i,10,10);
        vSnakeRect[vSnakeRect.size()-1-i]=rect;
    }

    timer=new QTimer(this);//设定计时器
    timer->start(Speed);//初始化刷新频率（移动速度）
    connect(timer,SIGNAL(timeout()),SLOT(Snake_update()));//槽函数



}
void Snake::Snake_update(){
    Display="";
    SnakeHead=vSnakeRect.first();//获取蛇头
    IsEat();
    IsHit();
    for(int j=0;j<vSnakeRect.size()-1;j++){
        vSnakeRect[vSnakeRect.size()-1-j]=vSnakeRect[vSnakeRect.size()-2-j];
    }
    switch(nDirection){
    case 1:
        SnakeHead.setTop(SnakeHead.top()-10);
        SnakeHead.setBottom(SnakeHead.bottom()-10);
        break;
    case 2:
        SnakeHead.setTop(SnakeHead.top()+10);
        SnakeHead.setBottom(SnakeHead.bottom()+10);
        break;
    case 3:
        SnakeHead.setLeft(SnakeHead.left()-10);
        SnakeHead.setRight(SnakeHead.right()-10);
        break;
    case 4:
        SnakeHead.setLeft(SnakeHead.left()+10);
        SnakeHead.setRight(SnakeHead.right()+10);
        break;
    default:;
    }
    vSnakeRect[0]=SnakeHead;//7
    if(SnakeHead.left()<20||SnakeHead.right()>270||SnakeHead.top()<20||SnakeHead.bottom()>270){//8
        blsOver=true;
        Display="游戏结束！";
    }
    update();//更新
}
void Snake::keyPressEvent(QKeyEvent*event){
    QKeyEvent *key=(QKeyEvent*) event;
    switch(key->key()){
    case Qt::Key_Up:nDirection=1;
        break;
    case Qt::Key_Down:nDirection=2;
        break;
    case Qt::Key_Left:nDirection=3;
        break;
    case Qt::Key_Right:nDirection=4;
        break;
    default:;
    }
    Snake_update();
}

QRect Snake::CreateFood(){
    int x,y;
    x=rand()%25;
    y=rand()%25;
    QRect rect(20+x*10,20+y*10,10,10);
    return rect;
}
void Snake::IsEat(){
    if(SnakeHead==Food){
        SnakeHead=Food;
        vSnakeRect.push_back(vSnakeRect.last());
        Food=CreateFood();
        Score++;
        SpeedChange();
    }
}
void Snake::IsHit(){
    for(int i=1;i<vSnakeRect.size();i++){
        if(SnakeHead==vSnakeRect[i]){
            Display="游戏结束！";
            blsOver=true;
            update();
        }
    }
}
void Snake::SpeedChange(){
    if(Speed>=300){
        Speed-=50;
        timer->stop();//停止当前计时器
        timer->start(Speed);
    }
    if(Speed<300&&Speed>=200){
        Speed-=10;
        timer->stop();//停止当前计时器
        timer->start(Speed);
    }
    if(Speed<200&&Speed>=100){
        Speed-=5;
        timer->stop();//停止当前计时器
        timer->start(Speed);
    }
}


